package nccloud.mob.riart.message.actions;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import itf.approvecenter.util.DataExchangeBean;
import nc.itf.uap.IUAPQueryBS;
import nc.vo.jcom.lang.StringUtil;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.BusinessException;
import nc.vo.pub.workflownote.WorkflownoteVO;
import nc.vo.wfengine.pub.WfTaskOrInstanceStatus;
import nc.vo.workflow.admin.WorkflowManageContext;
import nccloud.base.exception.ExceptionUtils;
import nccloud.commons.lang.StringUtils;
import nccloud.framework.mob.action.itf.IMobileAction;
import nccloud.framework.mob.container.IMobRequest;
import nccloud.framework.mob.container.MobClientInfo;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.action.itf.ICommonAction;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.container.SessionContext;
import nccloud.itf.uap.pf.NCCWorkFlowService;
import nccloud.web.workflow.approvalcenter.action.PfChecknoteEnum;
import nccloud.web.workflow.approve.util.NCCFlowUtils;
import nccloud.web.workflow.approve.util.NCCMsgContext;
import nccloud.web.workflow.approve.util.WorkflowManageUtil;










public class ApproveTransferAction
implements ICommonAction, IMobileAction
{
	WorkflownoteVO note = null;



	@Override
	public Object doAction(IRequest request) {
		String jsonStr = request.read();
		String cuserid = SessionContext.getInstance().getClientInfo().getUserid();

		return doApproveTransfer(jsonStr, cuserid, false);
	}




	@Override
	public Object doAction(IMobRequest req) {
		String jsonStr = req.read();

		String cuserid = (new MobClientInfo()).getUserid();

		 return doApproveTransfer(jsonStr, cuserid, true);
	}



	private Object doApproveTransfer(String jsonStr, String cuserid, boolean isMobile) {
		     DataExchangeBean bean = new DataExchangeBean();
		     bean.setCode("200");

		     JSONObject json2 = JSON.parseObject(jsonStr);

		     boolean isTrack = false;
		     if (json2.get("isTrack") != null) {
		    isTrack = json2.getBoolean("isTrack").booleanValue();
		}


		     NCCMsgContext nccMsg = new NCCMsgContext(jsonStr);

		     Map<String, Object> msgInfoMap = nccMsg.getMsgINfoContext();


		     String[] flowParameterArray = { "billtype", "billid", "pk_checkflow", "trans_user" };


		     if (NCCFlowUtils.isFlowParameterArrayNull(flowParameterArray, msgInfoMap)) {
		    return NCCFlowUtils.exchangeDataMsg(bean, "500",
					NCLangRes4VoTransl.getNCLangRes().getStrByID("0020nccloud_0", "0ncc0020001-0428"));
		}

		     String billType = msgInfoMap.get("billtype").toString();
		     String billId = msgInfoMap.get("billid").toString();
		     String pk_checkflow = msgInfoMap.get("pk_checkflow").toString();

		     NCCFlowUtils.checkIsExistMessage(billType, billId, cuserid);

		     String trans_user = (String)((List)json2.get("trans_user")).get(0);

		     String checknote = null;
		     if (msgInfoMap.get("checknote") != null) {
		       checknote = msgInfoMap.get("checknote").toString();
		}

		     NCCWorkFlowService nccWorkFlowService = ServiceLocator.find(NCCWorkFlowService.class);
		     AggregatedValueObject billvo = null;


		try {
			//			/* 110 */       if (!isMobile &&
			//					/* 111 */         StringUtils.isNotBlank(pk_checkflow)) {
			//				/* 112 */         String pk_group = nccWorkFlowService.getGroupByPKCheckFlow(pk_checkflow);
			//				/* 113 */         if (StringUtils.isNotBlank(pk_group)) {
			//					/* 114 */           SessionContext.getInstance().getClientInfo().setPk_group(pk_group);
			//				}
			//			}
			this.note = nccWorkFlowService.checkWorkflowActions(billType, billId, pk_checkflow);
			       NCCFlowUtils.checkIsCopySend(this.note, JSON.parseObject(jsonStr));

			       this.note.setTrack(isTrack);

			       Collection wps = ServiceLocator.find(IUAPQueryBS.class).retrieveByClause(nc.vo.pub.workflowpsn.WorkflowpersonVO.class, "pk_cuserid='" + trans_user + "'");

			       if (wps != null && !wps.isEmpty() && !json2.getBoolean("force_trans").booleanValue()) {
				         JSONObject retjson = new JSONObject();
				         retjson.put("code", "300");
				         retjson.put("content", NCLangRes4VoTransl.getNCLangRes().getStrByID("pfworkflow", "UPPpfworkflow-000708"));



			       return retjson;
			}

			      if (StringUtils.isNotBlank(cuserid) && cuserid.equals(trans_user)) {
			        throw new BusinessException(
						           NCLangRes4VoTransl.getNCLangRes().getStrByID("pfworkflow", "UPPpfworkflow-000014"));
			}

			     runTransfer(this.note, trans_user, checknote);
		    } catch (BusinessException e) {
			       ExceptionUtils.wrapException(e);
		}



		     if (StringUtil.isEmpty(checknote)) {
			       checknote = NCCFlowUtils.getIndividualCheckNote(PfChecknoteEnum.TRANSFER, cuserid);
		}

		     return JSON.toJSON(bean);
	}



	private boolean runTransfer(WorkflownoteVO worknoteVO, String trans_user, String checknote) {
		     WorkflowManageContext context = new WorkflowManageContext();
		     context.setBillId(this.note.getBillVersionPK());
		     context.setApproveStatus(Integer.valueOf(WfTaskOrInstanceStatus.Started.getIntValue()));
		     context.setSendman(this.note.getCheckman());
		     context.setWorkflownotePk(this.note.getPk_checkflow());
		     context.setBillType(this.note.getPk_billtype());
		     context.setFlowType(this.note.getWorkflow_type());


		     if ((new WorkflowManageUtil()).doAlterSend(context, trans_user, checknote)) {
			       worknoteVO.getTaskInfo().getTask().setAppoint(true);
			       return true;
		}
		return false;
	}
}
